// pages/my/my.js
const SERVICE = require("../../service.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    let token = wx.getStorageSync("token");    
    SERVICE.getQRcode({ type: 0, openid: token}, (status, data) => {
      if (status) {
        _this.setData({
          QRcodeImgSrc: data.data
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    let token = wx.getStorageSync("token");    
    wx.showNavigationBarLoading();
    SERVICE.getUserInfo({openid:token},(status,data)=>{
      wx.hideNavigationBarLoading();
      if (status){
        _this.setData({
          userInfo:data
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  updateInfo(){
    wx.navigateTo({
      url: '../datum-update/datum-update',
    });
  },
  ringUp(e){
    let mobile = e.currentTarget.dataset.mobile;
    wx.showModal({
      title: "",
      content: "客服热线" + mobile ,
      confirmColor: "#31AEFE",
      success:function(res){
        if(res.confirm){
          wx.makePhoneCall({
            phoneNumber: mobile,
            success: function () {
              console.log("拨打电话成功！")
            },
            fail: function () {
              console.log("拨打电话失败！")
            }
          });
        }
      }
    })
   
  },
  viewAgency(){
    let td  = this.data;
    if (td.userInfo.is_agent == 0){
      wx.navigateTo({
        url: "../reg-agency/reg-agency",
      })
    }else{
      wx.switchTab({
        url: '../agency/agency',
      })
    }
  },
  qrCode(){
    this.setData({
      showQRcode:true
    });
  },
  longpress(){
    let td = this.data;
   wx.showModal({
     title: "",
     content: "是否保存图片相册",
     success:function(res){
        if(res.confirm){
          wx.showLoading({
            title: "下载中...",
          });
          wx.downloadFile({
            url: td.QRcodeImgSrc,
            success: function (res) {
              wx.hideLoading();
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success: function (saveImg) {
                  wx.showToast({
                    title: "保存成功",
                    icon:"none",
                    mask:true
                  });
                },
                fail(saveImg) {
                  console.log(saveImg);
                }
              })
            }
          });
        }
     }
   })
  }
})